home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2269 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.4 KB  |  53 lines

  1. Path: rain.fr!world-net!usenet
  2. From: Frederic LACHASSE <lachass@worldnet.fr>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: ostrstreams
  5. Date: Tue, 16 Jan 1996 19:34:25 +0000
  6. Organization: World-Net information exchange, Internet provider.
  7. Message-ID: <VA.0000000d.00290fd5@fred>
  8. References: <4de6g3$e6u@saturn.ball.com>
  9. Reply-To: lachass@worldnet.fr
  10. NNTP-Posting-Host: client75.sct.fr
  11. X-Newsreader: Virtual Access by Ashmount Research Ltd, http://www.ashmount.com
  12.  
  13.  
  14. jsaler@ball.com (Jeff Saler) wrote:
  15. > I am using a function which requires an ostream reference as one of 
  16. > its parameters. After each call, I extract a string using the str() 
  17. > function.  I would like to have the next string that the function 
  18. > inserts into the ostream to overwrite the previous one instead of
  19. > being appended.  I tried calling flush(), but this had
  20. > no effect.
  21.  
  22. Use seekp(0), but don't forget to unfreeze the strstreambuf before as 
  23. str() freezes it.
  24.  
  25. example:
  26.  
  27.  ostrstream os;
  28.  
  29.  // output operation:
  30.  os << ...;
  31.  
  32.  // generally, I put a null byte at the end of the buffer
  33.  os << ends;
  34.  
  35.  // getting a pointer to the buffer
  36.  char *buf = os.str();
  37.  
  38.  // use buf
  39.  ...
  40.  
  41.  // str() freezes the strstreambuf, so that the pointer returned
  42.  // won't change, so we must unfreeze it
  43.  // note: buf should not be used after that
  44.  os.rdbuf()->freeze(0);
  45.  
  46.  // then reset the stream
  47.  os.seekp(0);
  48.  
  49.  Frederic LACHASSE (ECP 86)
  50.  CompuServe: 100530,2005
  51.  Internet: lachass@worldnet.fr
  52.  
  53.